<?php

namespace App\Models;

use App\Common\Constant\Params;
use App\Services\IStatisticService;
use App\Services\IStorageService;
use App\Services\IAreaService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Collection;

class Master extends BaseModel
{

    use Notifiable;

    protected $table = 'master';

    const CREATED_AT = 'add_time';
    const UPDATED_AT = 'upd_time';

    protected $dateFormat = 'U';

    protected $fillable = [
        'id',
        'weixin_openid',
        'weixin_userid',
        'phone',
        'real_name',
        'head_img',
        'idcard',
        'area_id',
        'area_text',
        'service_area_ids',
        'service_category',
        'service_type',
        'idcard_front',
        'idcard_back',
        'idcard_hold',
        'status',
        'disable_time'
    ];

//    protected $hidden = ['weixin_openid'];

    protected $casts = [
        'disable_time'=>'datetime'
    ];

    protected $appends = ['head_img_url'];

    public function setServiceTypeAttribute(array $value)
    {
        $this->attributes['service_type'] = implode(',',$value);
    }

    public function getServiceTypeAttribute($value)
    {
        return explode(',',$value);
    }

    public function setServiceAreaIdsAttribute(array $value)
    {
        $this->attributes['service_area_ids'] = implode(',',$value);
    }

    public function getServiceAreaIdsAttribute($value)
    {
        return explode(',',$value);
    }

    public function detail(){
        return $this->hasOne(MasterDetail::class,'master_id');
    }

    public function wallet(){
        return $this->hasOne(MasterWallet::class,'master_id');
    }

    public function cashes(){
        return $this->hasMany(MasterCash::class,'master_id');
    }

    public function stat(){
        return $this->hasOne(MasterStatistic::class,'master_id');
    }

    /**
     * 获取头像url绝对地址
     * @param  string  $value
     * @return string
     */
    public function getHeadImgUrlAttribute()
    {
        if (empty($this->head_img)){
            return asset('images/default_head.png');
        }else{
            return resolve(IStorageService::class)->getUrl($this->head_img);
        }
    }

    /**
     * 获取服务类目文本
     */
    public function getServiceCategoryTextAttribute()
    {
        return Params::$SERVICE_CATEGORY[$this->service_category];
    }

    /**
     * 获取所在地区面包屑
     */
    public function getAreaCrumbAttribute()
    {
        return resolve(IAreaService::class)->getAreaCrumbById($this->area_id,'-');
    }

    /**
     * 获取服务类型文本
     */
    public function getServiceTypeTextAttribute()
    {
        return Collection::make($this->service_type)->transform(function($item,$key){
            return @Params::$SERVICE_TYPE[$item];
        })->filter(function ($value, $key) {
            return $value;
        })->implode('/');
    }

    /**
     * 获取服务区域文本
     */
    public function getServiceAreasTextAttribute()
    {
        $areas = getAreas();
        return Collection::make($this->service_area_ids)->transform(function($item,$key) use ($areas){
            return $areas[$item]['n'];
        })->filter(function ($value, $key) {
            return $value;
        })->implode('/');
    }

    /**
     * 获取所在城市名称
     * city_name
     */
    public function getCityNameAttribute()
    {
        $areas = getAreas();
        return @$areas[$areas[$this->area_id]['p']]['n'] ? : '';
    }

    /**
     * 称呼，王师傅、吴师傅等
     * call_name
     */
    public function getCallNameAttribute()
    {
        return str_limit($this->real_name,2,'').'师傅';
    }

    /**
     * 获取身份证正面照片url绝对地址
     * @param  string  $value
     * @return string
     */
    public function getIdcardFrontUrlAttribute()
    {
        return resolve(IStorageService::class)->getUrl($this->idcard_front);
    }

    /**
     * 获取身份证背面照片url绝对地址
     * @param  string  $value
     * @return string
     */
    public function getIdcardBackUrlAttribute()
    {
        return resolve(IStorageService::class)->getUrl($this->idcard_back);
    }

    /**
     * 获取手持身份证半身照url绝对地址
     * @param  string  $value
     * @return string
     */
    public function getIdcardHoldUrlAttribute()
    {
        return resolve(IStorageService::class)->getUrl($this->idcard_hold);
    }

    /**
     * 师傅账户是否正在审核中
     * @param  string  $value
     * @return string
     */
    public function getWaitingPassAttribute()
    {
        return $this->status == Params::USER_STATUS_REVIEW;
    }

    /**
     * 师傅账户是否停单中
     * @param  string  $value
     * @return string
     */
    public function getDisabledAttribute()
    {
        return $this->status == Params::USER_STATUS_STOP;
    }
}
